<?php
/**
 * 配置控制器
 */
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use think\Request;

class AppconfigController extends Controller
{

    /**
     * 注册相关
     */
    public function registerconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 1",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            $Aliuploadimage = New Aliuploadimage();
            unset($param['edit_config']);
            //APPLOGO
            if (!empty($_FILES['applogourl']['name'])) {
                $file = request()->file('applogourl');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('APPLOGO图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['applogourl'] = $applogourlRes['data']['url'];
                }
            }
            //用户默认封面地址
            if (!empty($_FILES['membercoverimg']['name'])) {
                $file = request()->file('membercoverimg');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('用户默认封面地址图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['membercoverimg'] = $applogourlRes['data']['url'];
                }
            }
            foreach ($param as $k=>$v){
                if($k == 'isaftercancellation'){
                    if($v != 1 && $v != 2){
                        return admin_error('【注销后手机号可以重新注册】选择错误', URL_RELOAD);
                    }
                }
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【注册相关】配置成功', URL_RELOAD);
        }
    }


    /**
     * 短视频相关
     */
    public function videoconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 2",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            unset($param['edit_config']);
            foreach ($param as $k=>$v){
                if($k == 'isaftercancellation'){
                    if($v != 1 && $v != 2){
                        return admin_error('【注销后手机号可以重新注册】选择错误', URL_RELOAD);
                    }
                }
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【短视频相关】配置成功', URL_RELOAD);
        }
    }


    /**
     * 充值提现相关
     */
    public function rechargeconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 3",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            unset($param['edit_config']);
            foreach ($param as $k=>$v){
                if($k == 'isaftercancellation'){
                    if($v != 1 && $v != 2){
                        return admin_error('【注销后手机号可以重新注册】选择错误', URL_RELOAD);
                    }
                }
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【充值提现相关】配置成功', URL_RELOAD);
        }
    }

    /**
     * 奖励相关
     */
    public function awardconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            //邀请奖池现有油票数
            $Common = New Common();
            $invitationpoolnum = $Common->getConfigValue('invitationpoolnum');
            $invitationpoolnum = !empty($invitationpoolnum)?$invitationpoolnum:0;
            //圈层奖池现有油票数
            $circle_money = $IndexTableModel->getNum('fa_circle_list',"",'circle_money');
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 4",'*','config_sort asc,id asc');
            $this->assign([
                'data' => $data,
                'invitationpoolnum' => $invitationpoolnum,
                'circle_money' => $circle_money,
            ]);
            return $this->fetch();
        }else{
            unset($param['edit_config']);
            unset($param['invitationpoolnum']);
            unset($param['circle_money']);
            foreach ($param as $k=>$v){
                if($k != 'yqylword' && $k != 'watchtaskrule' && $k != 'fbspylrule'){
                    if(!is_numeric($v) || $v < 0){
                        return admin_error('请认真检查填写的信息是否正确', URL_RELOAD);
                    }
                }
            }
            foreach ($param as $k=>$v){
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【奖励相关】配置成功', URL_RELOAD);
        }
    }

    /**
     * 其他配置
     */
    public function otherconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 5",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            $Aliuploadimage = New Aliuploadimage();
            unset($param['edit_config']);
            //企业认证营业执照模版图片
            if (!empty($_FILES['qyrzyyzzurl']['name'])) {
                $file = request()->file('qyrzyyzzurl');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('企业认证营业执照模版图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['qyrzyyzzurl'] = $applogourlRes['data']['url'];
                }
            }
            //企业认证认证公函模版图片
            if (!empty($_FILES['qyrzghurl']['name'])) {
                $file = request()->file('qyrzghurl');//获取图片流文件
                $applogourlRes = $Aliuploadimage->setImages($file);
                if($applogourlRes['code'] != 200){
                    return admin_error('企业认证认证公函模版图片上传失败:'.$applogourlRes['msg'], URL_RELOAD);
                }else{
                    $param['qyrzghurl'] = $applogourlRes['data']['url'];
                }
            }
            foreach ($param as $k=>$v){
                if($k == 'isaftercancellation'){
                    if($v != 1 && $v != 2){
                        return admin_error('【注销后手机号可以重新注册】选择错误', URL_RELOAD);
                    }
                }
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【其他配置】成功', URL_RELOAD);
        }
    }


    /**
     * 版本配置
     */
    public function editionconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 6",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            foreach ($param as $k=>$v){
                if($k == 'android_new_edition' || $k == 'ios_new_edition'){
                    if(empty($v)){
                        return admin_error('【最新版本号】必须填写', URL_RELOAD);
                    }
                }
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【版本配置】成功', URL_RELOAD);
        }
    }



    /**
     * 私聊相关
     */
    public function privatechatconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_type` = 7",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            foreach ($param as $k=>$v){
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【私聊相关配置】成功', URL_RELOAD);
        }
    }


    /**
     * 公共配置
     */
    public function ggconfig(Request $request)
    {
        $IndexTableModel = New IndexTableModel();
        $param = $request->param();
        if(!isset($param['edit_config'])){
            $data = $IndexTableModel->getList('fa_config',"`config_key` in ('zbgirllb') or `config_type` = 8",'*','config_sort asc');
            $this->assign([
                'data' => $data,
            ]);
            return $this->fetch();
        }else{
            foreach ($param as $k=>$v){
                $IndexTableModel->editInfo('fa_config',array('config_key'=>$k),array('config_value'=>$v));
            }
            //修改配置
            return admin_success('修改【公共配置】成功', URL_RELOAD);
        }
    }










}
